#include "test.h"

int winid;
int sndid = 0;
struct Data
{
	int len;
	uint8_t* data;
};
std::map<int, Data*> recvMap;

void test_send(TurnClient* client)
{
	uint8_t buf[20000];
	memcpy(buf, &sndid, 4);
	//if(sndid < 10000)
	client->SendTo(buf, sizeof(buf));
	sndid++;
}

void Process()
{
	while (true)
	{
		auto data = recvMap.find(winid);
		if (data == recvMap.end())
			break;
		int id = *(int*)(&data->second->data[0]);
		delete data->second->data;
		delete data->second;
		recvMap.erase(data);
		//printf("{%d}", winid);
		winid++;
	}
}

void test_recv(uint8_t* data, uint32_t len)
{
	int pktID = *(int*)(&data[0]);
	if (pktID == 0)
	{
		/*for (std::map<int, Data*>::iterator iter = recvMap.begin(); iter != recvMap.end(); iter++)
		{
			delete iter->second->data;
			delete iter->second;
		}*/
		//recvMap.clear();
		winid = 0;
	}
	Data* temp = new Data();
	temp->data = new uint8_t[len - 4];
	memcpy(temp->data, data + 4, len - 4);
	temp->len = len - 4;
	std::pair<std::map<int, Data*>::iterator, bool> insertPair;
	insertPair = recvMap.insert(std::map<int, Data*>::value_type(pktID, temp));
	if (!insertPair.second)
	{
		printf("============== Insert to sendMsgMap failed ==============\n");
		throw;
	}
	Process();
}
